package com.tools.cleanmaster.manager;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.utils.NetworkUtils;


public class RatingDialogManager {


    private static volatile RatingDialogManager sInstance;
    public boolean mIsShowNotifyDialog = false;


    private RatingDialogManager() {
    }

    public static RatingDialogManager getInstance() {
        if (sInstance == null) {
            synchronized (RatingDialogManager.class) {
                if (sInstance == null) {
                    return sInstance = new RatingDialogManager();
                }
            }
        }
        return sInstance;
    }

    public void showRatingDialog(Activity activity) {
        if (!activity.isFinishing()) {
            RatingDialog dlg = new RatingDialog(activity);
            dlg.show();
        }
    }

    public void resetData() {
        mIsShowNotifyDialog = false;
    }

    public static class RatingDialog extends Dialog implements View.OnClickListener {
        public enum Type {
            PHONE_ACCELERATE, TRASH_CLEAN_QUICK, TRASH_CLEAN_DEEP, TRASH_CLEAN_QUICK_AND_DEEP
        }

        private View mCloseView;
        private TextView mGoRateBtn;
        private TextView mRate5Start;

        public RatingDialog(Context context) {
            super(context, R.style.MyTheme_CustomDialog);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            setContentView(R.layout.rating_dialog_layout);
            setCanceledOnTouchOutside(false);
            mRate5Start = findViewById(R.id.give_5_start);
            mRate5Start.setText(Html.fromHtml(context.getString(R.string.rating_dialog_star_rating)));
            mCloseView = findViewById(R.id.rating_2_close_btn);
            mCloseView.setOnClickListener(this);
            mGoRateBtn = findViewById(R.id.rating_dialog_2_rate_action);
            mGoRateBtn.setOnClickListener(this);
        }

        public void show() {
            super.show();
            RatingDialogManager.getInstance().resetData();
        }

        @Override
        public void onClick(View v) {
            if (v == mCloseView) {
                dismiss();
            } else if (v == mGoRateBtn) {
                if (!NetworkUtils.isNetworkAvaialble(CleanApplication.getInstance())) {
                    Toast.makeText(CleanApplication.getInstance(), R.string.recommend_network_message, Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    CleanApplication.getInstance().startActivity(RateManager.getRateIntent(CleanApplication.getInstance()));
                    dismiss();
                } catch (Exception e) {
                    Toast.makeText(CleanApplication.getInstance(), R.string.mark_dialog_market_not_found, Toast.LENGTH_SHORT).show();
                    dismiss();
                }
            }
        }
    }
}
